<template>

  <div>
    <!--    <img alt="Vue logo" src="../assets/logo.png">-->
    <!--    <HelloWorld msg="Welcome to Your Vue.js App"/>-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="add">新增</el-button>

    </div>

    <el-table :data="tableData" style="width: 100%">

      <el-table-column prop="position" label="职位"/>
      <el-table-column prop="salary" label="薪水"/>
      <el-table-column prop="tel" label="联系方式"/>
      <el-table-column prop="time" label="入职日期"/>
      <el-table-column fixed="right" label="操作" width="100">
        <template #default="scope">
          <el-button @click="handleEdit(scope.row)" type="text">编辑</el-button>
          <el-popconfirm
              title="确定删除吗？" @confirm="handleDel(scope.row.id)">
            <template #reference>
              <el-button size="mini" type="text">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          @size-change="handlePageSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pageSize"
          layout="total,sizes,prev,pager,next,jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="提示" v-model="dialogVisible" width="30%">
      <el-form :model="form" label-width="120px">
        <el-form-item label="职位">
          <el-input v-model="form.position" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="薪水">
          <el-input v-model="form.salary" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="form.tel" style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="入职日期">
          <el-input v-model="form.time" style="width: 80%"></el-input>
        </el-form-item>

      </el-form>
      <template #footer>
      <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// @ is an alias to /src
// import HelloWorld from '@/components/HelloWorld.vue'
import request from "@/utils/request";
export default {
  name: 'ReportView',
  components: {

  },
  data(){
    return {
      currentPage: 1,
      pageSize: 5,
      total: 10,
      form: {},
      dialogVisible: false,
      search:'',
      tableData:[

      ]
    }
  },
  created() {
    this.list()
  },
  methods:{
    handleDel(id){
      request.delete("/api/statistic/del/" + id).then(res =>{
        if(res.code === "200"){
          this.$message({
            type: "success",
            message: "删除成功"
          })
        }else {
          this.$message({
            type: "error",
            message: res.msg
          })
        }
      })
      this.list()
    },
    handlePageSizeChange(pageSize){
      this.pageSize = pageSize
      this.list()
    },
    handleCurrentChange(pageNum){
      this.currentPage = pageNum
      this.list()
    },
    list(){
      request.get("/api/statistic/statisticByPage",{
        params:{
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }}).then(res =>{
        this.tableData = res.data.records
        this.total = res.data.total

      })
    },
    save(){
      if(this.form.id){
        request.put("/api/statistic/update",this.form).then(
            res =>{
              if(res.code === "200"){
                this.$message({
                  type: "success",
                  message: "更新成功"
                })
              }else {
                this.$message({
                  type: "error",
                  message: "更新失败"
                })
              }
              this.list()
              this.dialogVisible = false
            }
        )
      }else {
        request.post("/api/statistic/save",this.form).then(
            res => {
              this.dialogVisible = false
              this.list()
            }
        )
      }
    },
    handleEdit(row){
      this.form = JSON.parse(JSON.stringify(row))
      this.dialogVisible = true
    },
    add(){
      this.dialogVisible = true
      this.form = {}
    }
  }
}
</script>